package com.triones.api.controller.web;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.triones.api.entity.CryptoChainDO;
import com.triones.api.entity.CryptoCurrencyDO;
import com.triones.api.entity.vo.CryptoChainVO;
import com.triones.api.entity.vo.CryptoCurrencyVO;
import com.triones.api.service.CryptoChainAndCurrencyService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 加密货币公链和币种
 *
 * @author Lion Li
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/crypto/chainAndCurrency")
public class CryptoChainAndCurrencyController {

    private final CryptoChainAndCurrencyService cryptoChainAndCurrencyService;

    /**
     * 查询公链列表
     */
    @SaCheckPermission("system:cryptoChain:pageList")
    @GetMapping("/chain/pageList")
    public TableDataInfo<CryptoChainVO> list(CryptoChainDO dto, PageQuery pageQuery) {
        return cryptoChainAndCurrencyService.chainPageList(dto, pageQuery);
    }

    /**
     * 查询公链详细
     *
     * @param id
     */
    @SaCheckPermission("system:cryptoChain:query")
    @GetMapping(value = "/chain/{id}")
    public R<CryptoChainVO> getInfo(@PathVariable Long id) {
        return R.ok(cryptoChainAndCurrencyService.selectChainById(id));
    }

    /**
     * 新增公链
     */
    @SaCheckPermission("system:cryptoChain:add")
    @PostMapping("/chain")
    public R<Void> addChain(@Validated @RequestBody CryptoChainDO dto) {
        if (!cryptoChainAndCurrencyService.checkChainUnique(dto)) {
            return R.fail("新增公链'" + dto.getChain() + "'失败，公链已存在");
        }
        cryptoChainAndCurrencyService.addChain(dto);
        return R.ok();
    }

    /**
     * 修改公链
     */
    @SaCheckPermission("system:cryptoChain:edit")
    @PutMapping("chain")
    public R<Void> editChain(@Validated @RequestBody CryptoChainDO dto) {
        if (!cryptoChainAndCurrencyService.checkChainUnique(dto)) {
            return R.fail("修改公链'" + dto.getChain() + "'失败，公链已存在");
        }
        cryptoChainAndCurrencyService.editChain(dto);
        return R.ok();
    }

    /**
     * 删除公链
     * @param id
     */
    @SaCheckPermission("system:cryptoChain:remove")
    @DeleteMapping("/chain/{id}")
    public R<Void> removeChain(@PathVariable Long id) {
        cryptoChainAndCurrencyService.removeChain(id);
        return R.ok();
    }

    /**
     * 获取选择框列表
     */
    @GetMapping("/chain/optionSelect")
    public R<List<CryptoChainVO>> optionSelect() {
        List<CryptoChainVO> dictTypes = cryptoChainAndCurrencyService.selectChainAll();
        return R.ok(dictTypes);
    }

    /**
     * 查询币种列表
     */
    @SaCheckPermission("system:cryptoCurrency:pageList")
    @GetMapping("/currency/pageList")
    public TableDataInfo<CryptoCurrencyVO> currencyList(CryptoCurrencyDO dto, PageQuery pageQuery) {
        return cryptoChainAndCurrencyService.currencyList(dto, pageQuery);
    }

    /**
     * 查询币种详细
     * @param id
     */
    @SaCheckPermission("system:cryptoCurrency:query")
    @GetMapping(value = "/currency/{id}")
    public R<CryptoCurrencyVO> getCurrencyInfo(@PathVariable Long id) {
        return R.ok(cryptoChainAndCurrencyService.getCurrencyInfo(id));
    }

    /**
     * 新增币种
     */
    @SaCheckPermission("system:cryptoCurrency:add")
    @PostMapping("/currency")
    public R<Void> addCurrency(@Validated @RequestBody CryptoCurrencyDO dto) {
        if (!cryptoChainAndCurrencyService.checkCurrencyUnique(dto)) {
            return R.fail("新增币种'" + dto.getCcy() + "'失败，币种已存在");
        }
        cryptoChainAndCurrencyService.addCurrency(dto);
        return R.ok();
    }

    /**
     * 修改币种
     */
    @SaCheckPermission("system:cryptoCurrency:edit")
    @PutMapping("/currency")
    public R<Void> editCurrency(@Validated @RequestBody CryptoCurrencyDO dto) {
        if (!cryptoChainAndCurrencyService.checkCurrencyUnique(dto)) {
            return R.fail("修改币种'" + dto.getCcy() + "'失败，币种已存在");
        }
        cryptoChainAndCurrencyService.editCurrency(dto);
        return R.ok();
    }

    /**
     * 删除币种
     * @param id
     */
    @SaCheckPermission("system:cryptoCurrency:remove")
    @DeleteMapping("/currency/{id}")
    public R<Void> removeCurrency(@PathVariable Long id) {
        cryptoChainAndCurrencyService.removeCurrency(id);
        return R.ok();
    }
}
